package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /*
        查询当前用户的购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //1.获取当前用户的id
        Long userId = BaseContext.get();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
        //select * from shoppingCart where user_id=?
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(list);
    }
    /*
        添加购物车
     */
    @PostMapping("/add")
    public R add(@RequestBody  ShoppingCart shoppingCart){
        //设置当前用户id
        shoppingCart.setUserId(BaseContext.get());
        //判断当前用户购物车中，是否已经包含这道菜或者套餐
        //select * from shoppingcart where userId=? and dish_id=?
        //如果没有，num设置为1，再保存  insert语句
        //如果已经有了，num自增，再修改num的值到数据库  update语句
        /*
            1.查询购物车中是否已经有本次的菜品或者套餐
         */
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        if(shoppingCart.getDishId()!=null){
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingForMysql = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        /*
            如果没有就新增，有就修改number数量+1
         */
        if(shoppingForMysql==null){
            //首次添加这道菜
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }else{
            //这道菜不是首次添加，修改数量
            shoppingForMysql.setNumber(shoppingForMysql.getNumber()+1);
            shoppingCartService.updateById(shoppingForMysql);
            return R.success(shoppingForMysql);
        }
    }

    /*
        减少购物车
        1.修改当前菜品或套餐的number数量-1
        2.如果number数量-1等于0，则删除这条数据
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.get());
        if(shoppingCart.getDishId()!=null){
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //1.查询数据库中的当前这个购物项
        ShoppingCart shoppingCartForMysql = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        //2.减少number数量
        shoppingCartForMysql.setNumber(shoppingCartForMysql.getNumber()-1);
        if (shoppingCartForMysql.getNumber()==0){
            //删除
            shoppingCartService.removeById(shoppingCartForMysql.getId());
        }else{
            //修改
            shoppingCartService.updateById(shoppingCartForMysql);
        }
        return R.success(shoppingCartForMysql);
    }

    /*
        清空当前用户的购物车
     */
    @DeleteMapping("/clean")
    public R clean(){
        LambdaUpdateWrapper<ShoppingCart> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.get());

        shoppingCartService.remove(wrapper);//delete from shoppingCart where user_id=?

        return R.success(null);
    }
}

